package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class permissionController {

    @Reference
    private PermissionService permissionService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        //返回分页结果对象
        return pageResult;
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        //异常处理:如果服务当出错,直接返回一个页面
        //不会把错误信息传递到前台
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        //服务调用成功
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findAll")
    //用于回显
    public Result findAll() {
        //程序健壮性和安全性处理
        try {
            List<Permission> list = permissionService.findAll();
            //查询成功返回结果
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败返回错误信息
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }
        @RequestMapping("/findById")
        public Result edit(Integer id){
            //程序健壮性和安全性处理
            try {
                //根据id查询对应id的检查项数据
               Permission permission= permissionService.findById(id);
                //查询对应id的权限数据成功后返回
                return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,permission);
            } catch (Exception e) {
                e.printStackTrace();
                //查询对应id的检查项数据失败后返回信息
                return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
            }
        }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        //程序健壮性和安全性处理
        try {
            //调用permission进行修改
            permissionService.edit(permission);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑权限内容失败,返回错误信息
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        //编辑权限内容成功,返回成功信息
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        //程序健壮性和安全性处理
        try {
            permissionService.delete(id);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }

        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

}
